<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
use Home\Model\ContentCollectModel;
class AlarmController extends Controller {

    	
     //报警列表和报警查找
    public function alarmList(){
    	if(session('name')){
    		$map = array();
    	if(!empty(I('param.key',''))){
    		$map['text_content'] = array('like','%'.I('param.key','').'%');
    	} 
    	$map['flag'] = 1;
    	$map['alert'] = 1;
    	$alarmTable = D('ContentCollect');
    	$alarmTable->mySelect(session('uid'));
    	$alarmTable->field(array('spider_content_collect.*','spider_alert_rule.rule_alert','spider_task.task_name'))->order('time desc')->join('spider_alert_rule ON spider_alert_rule.task_id = spider_content_collect.task_id','LEFT')->join('spider_task ON spider_task.id = spider_content_collect.task_id','LEFT')->relation(true)->where($map);
    	if(!empty(I('param.page','')) && !empty(I('param.pageCount',''))){
    		$alarmTable->page(I('param.page'),I('param.pageCount'));
    	}
    	$alarm = $alarmTable->select();
    	echo json_encode(array('result'=>$alarm,'count'=>$alarmTable->where($map)->count(),'code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    
    //根据id得到报警的类容
    public function alarmContentById(){
    	if(session('name')){
    		$map = array();
    		$contentTable = M('ContentCollect');
    		$content = $contentTable->where(array('id'=>I('param.id')))->select();
    		echo json_encode(array('result'=>$content,'code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    
    //得到所有的采集内容
    public function contentList(){
    	if(session('name')){
    		$map = array();
    		$alarmTable = D('ContentCollect');
    		$alarmTable->mySelect(session('uid'));
    		$alarmTable->field(array('spider_content_collect.*','spider_alert_rule.rule_alert','spider_task.task_name'))->join('spider_alert_rule ON spider_alert_rule.task_id = spider_content_collect.task_id','LEFT')->join('spider_task ON spider_task.id = spider_content_collect.task_id','LEFT')->relation(true)->where($map);
    		if(!empty(I('param.page','')) && !empty(I('param.pageCount',''))){
    			$alarmTable->page(I('param.page'),I('param.pageCount'));
    		}
    		$alarm = $alarmTable->order('time desc')->select();
    		echo json_encode(array('result'=>$alarm,'count'=>$alarmTable->count(),'code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
   
}